package mipushdemo.xiaomi.com.diyitonghang.model.impl.loginimpl;

import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import mipushdemo.xiaomi.com.diyitonghang.api.ApiService;
import mipushdemo.xiaomi.com.diyitonghang.data.bean.LoginBean;
import mipushdemo.xiaomi.com.diyitonghang.model.imodel.loginmodel.IMaModel;
import mipushdemo.xiaomi.com.diyitonghang.ui.activity.LoginActivity;
import mipushdemo.xiaomi.com.diyitonghang.ui.activity.MainActivity;
import mipushdemo.xiaomi.com.diyitonghang.utils.RetrofitUtils;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class MaModelImpl implements IMaModel {
    @Override
    public void responseDataMa(String header,String phone, final SetOnDataChangedListener listener) {
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("phone", phone);
            jsonObject1.put("smsType", "0");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse(header), jsonObject1.toString());
        ApiService apiService = RetrofitUtils.newInstance().netWorkRequest();
        Observable<ResponseBody> loginData1 = apiService.getYanZhengMa(header, requestBody);
        loginData1.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {

                        try {
                            String string = responseBody.string();
                            JSONObject object = new JSONObject(string);
                            String message = object.getString("message");
                            listener.setOnData(message);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void responseLoginData(String header, String phone, String yanZhengMa, final SetOnDataChangedListener1 listener) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("phone", phone);
            jsonObject.put("verificationCode", yanZhengMa);
            jsonObject.put("platform", "1");
            jsonObject.put("appVersion", "1.0.0");
            jsonObject.put("deviceId", "12312312");
            jsonObject.put("smsType", "0");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse(header), jsonObject.toString());
        ApiService apiService = RetrofitUtils.newInstance().netWorkRequest();
        Observable<LoginBean> loginData = apiService.phoneLogin(header, requestBody);
        loginData.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LoginBean loginBean) {
                            listener.setOnData1(loginBean);
                    }

                    @Override
                    public void onError(Throwable e)
                    {
                        Log.e("ERROR",e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
